<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="清洗计划名称" prop="cplanName">
        <el-input v-model="formData.cplanName" placeholder="请输入餐具清洗计划名称" />
      </el-form-item>
      <el-form-item v-if="formType === 'update'" label="清洗计划编号" prop="cplanCode">
        <el-input v-model="formData.cplanCode" placeholder="请输入餐具清洗计划编号" />
      </el-form-item>
      <el-form-item label="关联航班号" prop="flightNo">
        <el-input v-model="formData.flightNo" placeholder="请输入关联航班号" />
      </el-form-item>
      <el-form-item label="餐具" prop="tablewareType">
        <el-select v-model="formData.tablewareId" placeholder="请选择餐具" class="w-1/1">
          <el-option
            v-for="simple in SimpleList"
            :key="simple.id"
            :label="simple.name"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="餐具数量(个)" prop="tablewareQty">
        <el-input-number
          v-model="formData.tablewareQty"
          :min="0"
          controls-position="right"
          placeholder="请输入餐具数量(个)"
        />
      </el-form-item>
      <el-form-item label="清洗状态" prop="cleaningStatus">
        <el-select v-model="formData.cleaningStatus" placeholder="请选择清洗状态" class="w-1/1">
          <el-option
            v-for="item in getStrDictOptions(DICT_TYPE.ACM_CLEANING_STATUS)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="特别说明" prop="specialRemarks">
        <el-input
          type="textarea"
          :rows="4"
          maxlength="400"
          show-word-limit
          v-model="formData.specialRemarks"
          placeholder="请输入特别说明"
        />
      </el-form-item>
      <el-form-item label="计划有效期" prop="expireDate">
        <el-date-picker
          v-model="formData.expireDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TwCleaningPlanApi, TwCleaningPlanVO } from '@/api/pplan/twcleaningplan'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { TablewareApi } from '@/api/pplan/tableware'

/** 生产计划-餐具清洗计划 表单 */
defineOptions({ name: 'TwCleaningPlanForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  cplanName: undefined,
  cplanCode: undefined,
  flightNo: undefined,
  tablewareId: undefined,
  tablewareQty: undefined,
  cleaningStatus: undefined,
  specialRemarks: undefined,
  startDate: undefined,
  endDate: undefined,
  remarks: undefined,
  expireDate: []
})
const formRules = reactive({
  cplanName: [{ required: true, message: '餐具清洗计划名称不能为空', trigger: 'blur' }],
  flightNo: [{ required: true, message: '关联航班号不能为空', trigger: 'blur' }],
  tablewareId: [{ required: true, message: '餐具不能为空', trigger: 'change' }],
  tablewareQty: [{ required: true, message: '餐具数量(个)不能为空', trigger: 'blur' }],
  cleaningStatus: [{ required: true, message: '清洗状态不能为空', trigger: 'change' }],
  startDate: [{ required: false, message: '计划有效期开始不能为空', trigger: 'blur' }],
  endDate: [{ required: false, message: '计划有效期结束不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const SimpleList = ref([])
const getSimpleList = async () => {
  try {
    SimpleList.value = await TablewareApi.getTablewareSimpleList('')
  } finally {
  }
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  await getSimpleList()
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TwCleaningPlanApi.getTwCleaningPlan(id)
      formData.value.expireDate = [formData.value.startDate, formData.value.endDate]
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as TwCleaningPlanVO
    if (formType.value === 'create') {
      await TwCleaningPlanApi.createTwCleaningPlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await TwCleaningPlanApi.updateTwCleaningPlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    cplanName: undefined,
    cplanCode: undefined,
    flightNo: undefined,
    tablewareId: undefined,
    tablewareQty: undefined,
    cleaningStatus: undefined,
    specialRemarks: undefined,
    startDate: undefined,
    endDate: undefined,
    remarks: undefined
  }
  formRef.value?.resetFields()
}
</script>
